package ltd.bugs.cute.mybatis.checker;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import ltd.bugs.cute.common.spi.checker.Checker;
import ltd.bugs.cute.common.spring.ApplicationContextHolder;

/**
 * 数据库连接池的监控，将连接池的当前状态打印到日志文件中
 * 
 * @author zhengzhq
 *
 */
public class DBPoolChecker implements Checker {

  private static Logger logger = LoggerFactory.getLogger(DBPoolChecker.class);

  @Override
  public void check() {
    ComboPooledDataSource dataSource =
        ApplicationContextHolder.getBean(ComboPooledDataSource.class);
    if (dataSource == null) {
      return;
    }

    try {
      logger.info("current connectinos {}, busy connections {}, idle connections {}",
          new Object[] {dataSource.getNumConnections(), dataSource.getNumBusyConnections(),
              dataSource.getNumIdleConnections()});
    } catch (Exception e) {
      logger.error("get db pool status error", e);
    }
  }
}
